import threading

from CompThread import compThread
import time
from dataPath import cleaned_filepath

#flag为传递的参数，keyWords为种子关键字数组，这里面创建多线程
def  creatCompThreads(flag, keyWords ,midkeynum):
    #########这里要进行改变,不知道为什么要判断，很奇怪
    file = open(cleaned_filepath, 'rb')
    # file读取了搜索日志，我们要根据种子关键词获取相关的搜索记录
    searchLog = file.readlines()
    threads = []
    start_time = time.time()
    #
    # 终于用到了互斥锁了,学的终于有用了，这里是创建锁
    #
    mutex = threading.Lock()
    #循环创建线程
    for i, seedKeyWord in enumerate(keyWords, 1):
        #flag传入使得线程使用哪个分词工具,seedKeyWord传入keyWords中一个种子关键字,mutex为共用的一把锁,防止公用文件时出现错误
            thread = compThread(i, "Thread-" + str(i), flag, searchLog, seedKeyWord, midkeynum, mutex)
            threads.append(thread)
            threads[i-1].start()
    #循环运行线程，并设置阻塞，就是要等子进程结束，才继续
    for i, k in enumerate(keyWords, 0):
        threads[i].join()
    file.close()
    end_time = time.time()
    print("竞争关键字计算执行时间为{:.4f}秒".format(end_time - start_time))



